//
//  SCDatePicker.m
//  Shuaiche
//
//  Created by 党帅 on 2020/7/8.
//  Copyright © 2020 ShuaiChe. All rights reserved.
//

#import "SCDatePicker.h"

typedef NS_OPTIONS(NSUInteger, SCDateDataOption) {
    SCDateDataOptionNone = 1 << 0,
    SCDateDataOptionYear = 1 << 2,
    SCDateDataOptionMonth = 1 << 3,
    SCDateDataOptionDay = 1 << 4,
    SCDateDataOptionHour = 1 << 5,
    SCDateDataOptionMinitus = 1 << 6,
    SCDateDataOptionSeconds = 1 << 7
};

@interface SCDatePicker ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic, copy) NSString *dateFormatter;
@property (nonatomic, strong) UIPickerView *pickerView;
//@property (nonatomic, assign) NSInteger componentsNum;
@property (nonatomic, assign) SCDateDataOption dataOptions;
@property (nonatomic, copy) NSString *currentDateString;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation SCDatePicker

- (instancetype)initWithDateFormatter:(NSString *)dateFormatter {
    self = [super init];
    if (self) {
        self.dateFormatter = dateFormatter;
    }
    return self;
}


- (void)setDateFormatter:(NSString *)dateFormatter {
    _dateFormatter = dateFormatter;
    NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:@"\\w+" options:kNilOptions error:nil];
    NSArray *array = [reg matchesInString:dateFormatter options:kNilOptions range:NSMakeRange(0, dateFormatter.length)];
//    _componentsNum = array.count;
    NSDictionary *infos = @{@"yyyy":@2,@"MM":@3,@"dd":@4,@"HH":@5,@"mm":@6,@"ss":@7};
    _dataOptions = 1;
    for (NSTextCheckingResult *result in array) {
        NSRange range = result.range;
        NSString *res = [dateFormatter substringWithRange:range];
        int v = [infos[res] intValue];
        _dataOptions = _dataOptions | (1 << v);
    }
    [self prepareDataSource];
}

- (void)prepareDataSource {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSInteger unitFlags  = _dataOptions;
    NSDateComponents *comps = [calendar components:unitFlags fromDate:[NSDate date]];
    comps = [calendar components:unitFlags fromDate:[NSDate date]];
    if (_dataOptions & SCDateDataOptionYear) {
        NSInteger year = [comps year];
        NSMutableArray *years = [NSMutableArray arrayWithCapacity:0];
        for (long i = year - 10; i < year + 10 ; i++) {
            [years addObject:[NSString stringWithFormat:@"%ld",i]];
        }
        [self.dataSource addObject:years];
    }
    [self.dataSource addObject:@[@"年"]];
    if (_dataOptions & (SCDateDataOptionMonth | SCDateDataOptionDay)) {
        NSMutableArray *months = [NSMutableArray arrayWithCapacity:12];
        for (int i = 1 ; i <= 12; i++) {
            NSString *monthStr = [NSString stringWithFormat:@"%d",i];
            [months addObject:monthStr];
        }
        [self.dataSource addObject:months];
        [self.dataSource addObject:@[@"月"]];
        NSMutableArray *days = [NSMutableArray arrayWithCapacity:31];
        for (int i = 1 ; i <= 31; i++) {
            NSString *monthStr = [NSString stringWithFormat:@"%d",i];
            [days addObject:monthStr];
        }
        [self.dataSource addObject:days];
        [self.dataSource addObject:@[@"日"]];
    }
    
    if (_dataOptions & SCDateDataOptionHour) {
        NSMutableArray *hours = [NSMutableArray arrayWithCapacity:24];
        for (int i = 0 ; i < 24; i++) {
            NSString *monthStr = [NSString stringWithFormat:@"%02d",i];
            [hours addObject:monthStr];
        }
        [self.dataSource addObject:hours];
        [self.dataSource addObject:@[@"时"]];
    }
    
    if (_dataOptions & SCDateDataOptionMinitus) {
        NSMutableArray *hours = [NSMutableArray arrayWithCapacity:60];
        for (int i = 0 ; i < 60; i++) {
            NSString *monthStr = [NSString stringWithFormat:@"%02d",i];
            [hours addObject:monthStr];
        }
        [self.dataSource addObject:hours];
        [self.dataSource addObject:@[@"分"]];
    }
    [self.pickerView reloadAllComponents];
}

- (UIPickerView *)pickerView {
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc] init];
        _pickerView.dataSource = self;
        _pickerView.delegate = self;
        [self addSubview:_pickerView];
        _pickerView.translatesAutoresizingMaskIntoConstraints = NO;
        [_pickerView.leftAnchor constraintEqualToAnchor:self.leftAnchor].active = YES;
        [_pickerView.rightAnchor constraintEqualToAnchor:self.rightAnchor].active = YES;
        [_pickerView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:-kSCScreenBottomOffset].active = YES;
        [_pickerView.heightAnchor constraintEqualToConstant:216].active = YES;
    }
    return _pickerView;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSource;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return _dataSource.count;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    NSArray *array = _dataSource[component];
    return array.count;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component % 2 == 1) {
        return 15;
    } else {
        NSInteger c = _dataSource.count / 2;
        return (kSCScreenWidth - c * 15) / c;
    }
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSArray *array = _dataSource[component];
    NSString *string = array[row];
    return string;
}

- (void)show {
    self.frame = CGRectMake(0, 0, kSCScreenWidth, kSCScreenHeight);
    [[UIApplication sharedApplication].keyWindow addSubview:self];
//    [UIView animateWithDuration:0.4 animations:^{
//        contentView.center = CGPointMake(self.frame.size.width/2, contentView.center.y - contentView.frame.size.height);
//    }];
}

@end
